package com.kilon.workfaq.rest;

import com.kilon.workfaq.entity.ImagePo;
import com.kilon.workfaq.service.read.ImageReadService;
import com.kilon.workfaq.service.write.ImageWriteService;
import com.kilon.workfaq.util.LocalUploadImageUtils;
import com.kilon.workfaq.util.ResultBean;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * 图片表(Image)表控制层
 *
 * @author makejava
 * @since 2023-02-22 23:24:10
 */
@RestController
@RequestMapping("image")
@CrossOrigin(origins = {"http://localhost:8080", "http://127.0.0.1:8080"}) //解决跨域问题
public class ImageRestService extends ResultBean {
    /**
     * 服务对象
     */
    @Resource
    private ImageReadService imageReadService;
    @Resource
    private ImageWriteService imageWriteService;

    //@Autowired
    //private LocalUploadImageUtils uploadPictureService;

    @PostMapping("/uPictureCheck")
    public ResultBean uPictureCheck(@RequestBody String titlemsg) {
        ResultBean rb = new ResultBean(400, "0");
        if (!"".equals(titlemsg)) {
            if (titlemsg.contains("kilon")) {
                rb.setCode(200);
                rb.setMessage("1");
            }
        }
        return rb;
    }

    //@ApiOperation("图片上传")
    @PostMapping("/uPictureV1")
    public ResultBean UploadPicture(MultipartFile file) {
        ResultBean rb = new ResultBean(200, "上传成功");
        String fileName = file.getOriginalFilename();
        //调用工具类的方法
        //String imageFile = LocalUploadImageUtils.uploadImageFile(file,fileName);//替换图片，业务不合理
        String imageFile = LocalUploadImageUtils.uploadImageFile(file);//随机字符串，合理
        if (!imageFile.isEmpty()) {
            rb.setData(imageFile);
        } else {
            rb.setCode(400);
            rb.setMessage("上传出错");
        }
        return rb;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("selectOne")
    public ImagePo selectOne(String id) {
        return this.imageReadService.queryById(id);
    }

    @PostMapping("queryAll")
    public List<ImagePo> selectAll(ImagePo imagePo) {
        return this.imageReadService.queryAll(imagePo);
    }

    /**
     * 新增项目
     *
     * @return
     */
    @PostMapping("addImage")
    public void addImage(ImagePo imagePo) {
        //这里添加自定义条件
        this.imageWriteService.insert(imagePo);
    }

    /**
     * 更新项目
     *
     * @return
     */
    @PostMapping("updateProject")
    public void updateImage(ImagePo imagePo) {
        //这里添加自定义条件
        if (this.imageWriteService.update(imagePo)) {
            //setResultData("更新成功");
        } else {
            //setErrorMsg("更新出错");
        }
    }

    /**
     * 删除项目
     *
     * @return
     */
    @PostMapping("dropImage")
    public void dropImage(ImagePo imagePo) {
        if (this.imageWriteService.deleteById(imagePo)) {
            //setResultData("删除成功");
        } else {
            //setErrorMsg("删除出错");
        }
    }
}
